package com.bkitmobile.kesu.appengine.web.client.SupportPopup;

import org.datanucleus.store.mapped.MappedTypeManager;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;


public class GoogleMap extends HTML{
//	private static int nextId = 0;
	private JavaScriptObject map;
	private JavaScriptObject listener = null;
	private String id = "map";      //+(++nextId);
	public GoogleMap(){
		
		setHTML("<div id='"+getId()+"' style='height: 550px'></div>");
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setMap (JavaScriptObject map)
	{
		this.map=map;
	}
	
	public JavaScriptObject getMap ()
	{
		return this.map;
	}
	
	///// Kiem tra xem 1 kieu co phai la private khong
	public boolean isPrivate (String type)
	{
		if (type.equals("Bank") || type.equals("Cinema") || type.equals("Clothes")
			||type.equals("Coffee") || type.equals("Company") || type.equals("Computer")
			||type.equals("Factory")||type.equals("Hotel") || type.equals("Resort") 
			|| type.equals("Restaurant"))
			return true;
		else
			return false;
			
	}
	public void setCenter(final double lon,final double lat ){
		setCenterImpl( map, lon, lat );
	}
	
	public JavaScriptObject initMap (String id)
	{
		return this.initMapImp(id);
	}
	private native static JavaScriptObject initMapImp( String id )
	/*-{
		var t = $wnd.initialize();
		return t;
		
		//$wnd.alert("1");
		//var centerLatitude = 10.777881;
		//var centerLongitude = 106.653357;
		//var startZoom = 13;
		//$wnd.alert("2");
		//var latlng = new $wnd.google.maps.LatLng(centerLatitude, centerLongitude);
		//$wnd.alert("3");
    	//var options = {
     	//	zoom: $wnd.startZoom,
     	//	center: $wnd.latlng,
     	//	mapTypeId: $wnd.google.maps.MapTypeId.ROADMAP
    	//};
    	//$wnd.alert("4");
    	//var _ret = new $wnd.google.maps.Map($doc.getElementById("map"), options);
    	//$wnd.alert("5");
    	//return _ret;
	}-*/;
	private native static void setCenterImpl(
		JavaScriptObject map,final double lon,final double lat )/*-{
		map.setCenter(new $wnd.GLatLng(lon, lat), 13);
	}-*/;


	public void addMarker (double Lat, double Lon, String kind, boolean isPrivate,
			String name, String type, String info, String address)
	{
		String imgLnk = "images/";
		if (isPrivate)
		{
			imgLnk+="private/" + kind.toLowerCase() + ".png";
		}
		else
		{
			imgLnk+="public/" + kind.toLowerCase() + ".png";
		}
		//Window.alert(imgLnk);
		MarkerInfoWindow infomation = new MarkerInfoWindow(name, type, info, address);
		addMarkerImp(Lat, Lon,imgLnk, infomation.getHTML());
	}
	
	
	private native static void addMarkerImp (double Lat, double Lon, String img, String infoWinHTML)
	/*-{
		var marker = $wnd.addMarker(Lat,Lon,img,infoWinHTML);
	}-*/;
	
	private void createLocation (String lat, String lng)
	{
		//Window.alert(lat);
		//Window.alert(lng);
		
		double latitude = Double.parseDouble(lat);
		double longtitue = Double.parseDouble(lng);
		LocationInfoPopup inputLocation = new LocationInfoPopup(latitude,longtitue);
		inputLocation.show();
	}
	
	public void activateClickMap()
	{
		if (this.listener == null)
			this.listener = this.ClickMap(this);
	}
	
	private native static JavaScriptObject ClickMap(GoogleMap x)
	/*-{
		return $wnd.google.maps.event.addListener($wnd.map, 'click', function(event) {
			x.@com.bkitmobile.kesu.appengine.web.client.SupportPopup.GoogleMap::createLocation(Ljava/lang/String;Ljava/lang/String;)
				(event.latLng.lat().toString(),event.latLng.lng().toString());				
		});
		
	}-*/;
	
	public void deactivateClickMap()
	{
		if (this.listener!=null)
		{
			this.deactivateClickMapImp(this.listener);
			this.listener = null;
		}
	}

	private native static void deactivateClickMapImp(JavaScriptObject input) 
	/*-{
		$wnd.google.maps.event.removeListener(input);
	}-*/;
	
}
			
